package com.njtoyo.taxi.admin.rest.controller.open;

import com.njtoyo.taxi.admin.aop.LoggerManage;
import com.njtoyo.taxi.admin.aop.PassToken;
import com.njtoyo.taxi.admin.manager.AsyncManager;
import com.njtoyo.taxi.admin.manager.factory.AsyncFactory;
import com.njtoyo.taxi.admin.mapper.master.entity.CallEndSend;
import com.njtoyo.taxi.admin.rest.wrapper.open.privacy_number.FinishWrapper;
import com.njtoyo.taxi.admin.service.open.CallEndSendService;
import com.njtoyo.taxi.admin.service.open.PrivacyNumberService;
import com.njtoyo.taxi.admin.utils.StringUtils;
import com.taxi.entity.common.RestResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("open/privacy-number")
@Api(tags = {"隐私通话管理"})
@Slf4j
public class PrivacyNumberController {
    @Autowired
    private PrivacyNumberService privacyNumberService;
    @Autowired
    private CallEndSendService callEndSendService;

    @PostMapping("fee")
    @ApiOperation(value = "话单通知接口", notes = "话单通知接口，开放接口")
    @PassToken
    @LoggerManage(description = "话单通知接口")
    RestResult fee(@RequestBody FinishWrapper wrapper) {
        return privacyNumberService.feeNotify(wrapper);
    }

    @PostMapping("/v2/axb/finish")
    @ApiOperation(value = "呼叫结束推送", notes = "呼叫结束推送，开放接口")
    @PassToken
    @LoggerManage(description = "呼叫结束推送")
    Map callEndSend(@RequestBody CallEndSend callEndSend) {
        boolean ret =false;
        try {
            if(callEndSendService.save(callEndSend)){
                ret =true;
                if(StringUtils.isNotEmpty(callEndSend.getRecordUrl())){
                    //转存录音文件到天翼云
                    AsyncManager.me().execute(AsyncFactory.saveToCloud(callEndSend.getRecordUrl(),callEndSend.getCallid()));
                }
            }
        }catch (Exception e){
            log.error("呼叫结束推送异常:"+e.getMessage());
        }
        Map<String,String> map = new HashMap(2);
        if(ret){
            map.put("code","0");
            map.put("message","success");
        }else {
            map.put("code","503");
        }

        return map;
    }

}
